//
//  YGKShowFlagController.m
//  GatherFlag
//
//  Created by CoderSUX on 16/7/6.
//  Copyright © 2016年 CoderSUX. All rights reserved.
//

#import "YGKShowFlagController.h"
#import "HMDataManager.h"
#import "YGKFlagCell.h"
NSString *const FlagCell = @"FlagCell";
@interface YGKShowFlagController ()

@end

@implementation YGKShowFlagController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button = [[UIButton alloc] init];
    [button setTitle:@"关闭" forState:(UIControlStateNormal)];
    
    button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 80, 30, 60, 30);
    button.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:0.3];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
    
    self.fetchedResultsController.fetchRequest.predicate = nil;
    [self.fetchedResultsController performFetch:NULL];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"YGKFlagCell" bundle:nil] forCellReuseIdentifier:FlagCell];
}

- (void)close
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.fetchedResultsController.fetchedObjects.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YGKFlagCell *cell = [tableView dequeueReusableCellWithIdentifier:FlagCell forIndexPath:indexPath];
    
    Flag *flag = self.fetchedResultsController.fetchedObjects[indexPath.row];
    cell.flag = flag;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 240;
}


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        Flag *flag = self.fetchedResultsController.fetchedObjects[indexPath.row];
        [[HMDataManager sharedManager].managedObjectContext deleteObject:flag];
        [[HMDataManager sharedManager] saveContext];
        [self.fetchedResultsController performFetch:NULL];
        [self.tableView reloadData];
    }
}




@end
